package com.zhangpp.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangpp.controller.AdminController;
import com.zhangpp.dao.AdminMapper;
import com.zhangpp.entity.Admin;
import com.zhangpp.service.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;


@Service
@Transactional
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private AdminService adminService;
    @Resource
    HttpServletRequest request;
    private static final Logger log = LoggerFactory.getLogger(AdminController.class);
    public HashMap<String,Object> login(Admin admin,String enCode){

        HashMap<String, Object> map = new HashMap<>();
        String code = (String) request.getServletContext().getAttribute("code");
        log.info(code);
        if(code.equals(enCode)){
            if(!admin.equals("")){
                QueryWrapper<Admin> wrapper = new QueryWrapper<>();
                QueryWrapper<Admin> name = wrapper.eq("username", admin.getUsername());
                Admin one = adminService.getOne(name);
                if(one.getPassword().equals(admin.getPassword())){
                    map.put("message",admin);
                    map.put("status",200);
                }else{
                    map.put("message", "密码错误");
                }
            }else{
                map.put("message", "用户不存在");
            }
        }else{
            map.put("message", "验证码错误");
        }

        return map;
    }

}
